/**
 * @author monirul
 */

var UserListView = new Class.create(
		{

			
			initialize : function() {

			},

			init : function(o) {
				if (o['componentId'] != null)
					this._componentId = o['componentId'];
				if (o['model'] != null)
					this._model = o['model'];
				if (o['controller'] != null)
					this._controller = o['controller'];
				if (o['elements'] != null) {
					this._elements = o['elements'];
					this._targetLocation = this._elements['targetLocation'];
				}

				var init = {};
				this.userHistoryModel = ComponentManager.loadComponent( {
					componentId : 'userHistoryModel_instance',
					jsClassName : 'UserHistoryModel',
					initData : init
				});

				var userHistoryViewElements = {
					'targetLocation' : 'history'
				};
				var init = {
					'model' : this.userHistoryModel,
					'controller' : '',
					'elements' : userHistoryViewElements
				};
				this.userHistoryView = ComponentManager.loadComponent( {
					componentId : 'userHistoryView_instance',
					jsClassName : 'UserHistoryView',
					initData : init
				});

				var _this = this;
				// attach model listeners
				this._model.userListUpdated.attach(function() {
					_this.rebuildList();
				});
				// this._model.displayUserHistory.attach(function () {
				// _this.showHistoryWindow();
				// });

			},
			show : function() {
				this.rebuildList();
			},

			rebuildList : function() {
				this.users = new Array();
				this.height = 0;
				var userList = this._model.getUserList();
				for ( var i = 0; i < userList.length; i++) {
					var currRoom = "-";
					var roomOwner = "-";
					this.currRoom = "";
					if (userList[i].roomName != null) {
						this.currRoom = userList[i].roomName;
						// this.roomOwner = userList[i].currentRoom.ownerName;
					}
//					if (userList[i].currentRoom != null) {
//						this.currRoom = userList[i].currentRoom.roomName;
//						// this.roomOwner = userList[i].currentRoom.ownerName;
//					}
					// this.users[i] = {'userName':userList[i].name
					// ,'roomName':this.currRoom ,'owner':this.roomOwner}
					// this.userLink = '<a
					// onclick="alert(\'user\');componentExecFunctionWithCall(\''+this._componentId
					// +'\',\'showHistoryWindow\','+
					// userList[i].usetHistoryList+')"
					// >'+userList[i].name+'</a>'
					this.userLink = '<a  onclick="alert(\'user\');HistoryService.getUserHistory(\''
							+ userList[i].name
							+ '\',\'userHistoryModel_instance\')" >'
							+ userList[i].name + '</a>'
					// this.userLink = '<a
					// onclick="alert(\'user\');this.showHistoryWindow();"
					// >'+userList[i].name+'</a>'
					this.users[i] = {
						'userName' : this.userLink,
						'roomName' : this.currRoom
					}
					this.height += 25;
				}

				this.columnDefs = [ {
					key : "userName",
					label : "User Name",
					sortable : true
					,
					resizeable : true,
					width : 130
				}, {
					key : "roomName",
					label : "Room Name",
					sortable : true
					,
					resizeable : true,
					width : 100
				}
				// ,
				// {key:"owner",lebel:"Room Owner", resizeable:true}
				];
				this.rowFormatter = function(elTr, oRecord) {
					
					if (oRecord.getData('roomName').length > 0) {
						YAHOO.util.Dom.addClass(elTr, 'mark');
					}
					return true;
				};

				var init = {
					'colDef' : this.columnDefs,
					'title' : "User List",
					'targetPosition' : this._targetLocation
				};
				var userListTbl = ComponentManager.loadComponent( {
					componentId : 'userlist_table',
					jsClassName : 'DataTableWidget',
					initData : init
				});
				userListTbl.setRowFormatter(this.rowFormatter);
				userListTbl.drawTable(this.users);
			},

			showHistoryWindow : function(userHistory) {
				alert('user history');
				if (userHistory != null) {
					alert('user history lenght :  ' + userHistory.length);
				}
				window.open('www.google.com', 'myGoogle',
						'width=300,height=300,scrollbars=yes');
			}

		});
